﻿using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;

namespace TrackSystem.Entities.Extensions
{
    public class IP4AddressAttribute : ValidationAttribute
    {
        private const string Pattern = @"^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$";

        public IP4AddressAttribute() : base("{0} 地址格式无效")
        {
            
        }

        public override bool IsValid(object? value)
        {
            if (value == null) return false;
            var ip = value as string;
            return Regex.IsMatch(ip, Pattern);
        }
    }
}
